package com.southwind.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.southwind.entity.Order;
import com.southwind.service.ProviderService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class ProviderController {

    @Value("${server.port}")
    private String port;

    @GetMapping("index")
    public String index() {
        return "hello spring cloud alibaba " + port;
    }


    // sentinel测试============================================================================================
    @Autowired
    private ProviderService providerService;

    @GetMapping("test")
    public String test() {
        return "test";
    }

    @GetMapping("test1")
    public String test1() {
        providerService.test();
        return "test1";
    }

    @GetMapping("test2")
    public String test2() {
        providerService.test();
        return "test2";
    }

    @GetMapping("hot")
    @SentinelResource("hot") // 启用热点规则
    public String hot(Integer num1, Integer num2) {
        return num1 + "-" + num2;
    }

    @GetMapping("blankAndWhite")
    public String blankAndWhite() {
        return "黑名单/白名单";
    }


    // rockectmq测试===========================================================================================
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 向rocketmq发送消息
     */
    @GetMapping("/create")
    public Order create(){
        Order order = new Order(
                1,
                "张三",
                "123123",
                "软件园",
                new Date()
        );
        this.rocketMQTemplate.convertAndSend("myTopic", order);
        return order;
    }
}
